import { BasicColumn, BasicTable, FormSchema, TableAction, useTable } from "@/components/Table";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { message } from "antd";
import CreateAndUpdata from "./components/createAndUpdata";
import { useDrawer } from "@/components/Drawer";
import { useTranslation } from "react-i18next";
import { formatDate } from "@/utils/util";
import { useEffect, useState } from "react";
import { getListSimpleUsers } from "@/api/system/user";
import { IconEnum } from "@/enums/appEnum";
import { getSimpleDeptList } from "@/api/system/dept";
import { receiptsApi } from "@/api/statisticalReport/receipt";

const index = () => {
	const { t } = useTranslation();
	const en_vi = "statisticalReport.receipt";
	const [userList, setUserList] = useState<any>([]);
	const schemas: FormSchema[] = [
		{
			label: t(`${en_vi}.patient`),
			field: "patId",
			component: "Input"
		},
		{
			label: t(`erp.invtDept.department`),
			field: "deptId",
			component: "ApiSelect",
			componentProps: {
				api: getSimpleDeptList,
				resultField: "data.list",
				labelField: "name",
				valueField: "id"
			}
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.relatedOrder`),
			dataIndex: "orderId"
		},
		{
			title: t(`${en_vi}.patient`),
			dataIndex: "patId"
		},
		{
			title: t(`erp.invtDept.department`),
			dataIndex: "deptId"
		},
		{
			title: t(`${en_vi}.issuer`),
			dataIndex: "staffId",
			render: (text: string) => {
				const user = userList.find((item: any) => item.id == text);
				return user ? user.nickname : "";
			}
		},
		{
			title: t(`${en_vi}.issueDate`),
			dataIndex: "issueDate"
		},
		{
			title: t(`${en_vi}.paymentMethod`),
			dataIndex: "pmtMethod",
			render: (pmtMethod: any) => {
				if (pmtMethod == "0") return t(`${en_vi}.cash`);
				if (pmtMethod == "1") return t(`${en_vi}.socialSecurity`);
				if (pmtMethod == "2") return t(`${en_vi}.bankCard`);
				return t(`${en_vi}.others`);
			}
		},
		{
			title: t(`${en_vi}.amountReceived`),
			dataIndex: "amount"
		},
		{
			title: t(`${en_vi}.receiptNumber`),
			dataIndex: "receiptNo"
		},
		{
			title: t(`erp.invtDept.creationTime`),
			dataIndex: "createdTime",
			render: (time: any) => formatDate(new Date(time))
		},
		{
			title: t(`erp.warehouseGroupType.operations`),
			fixed: "right",
			width: 200,
			render: (_: any, record: any) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.edit"),
							onClick: updateFn(record)
						},
						{
							icon: IconEnum.DELETE,
							label: t("felix_ui.delete"),
							popConfirm: {
								title: t("common.deleteMessage"),
								placement: "left",
								confirm: deleteFn.bind(null, record)
							}
						}
					]}
				></TableAction>
			)
		}
	];
	const [registerTable, { reload }] = useTable({
		api: receiptsApi.receiptsList,
		columns,
		useSearchForm: true,
		formConfig: {
			labelWidth: 120,
			schemas,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 },
			actionColOptions: { span: 24 }
		},
		bordered: true
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	const handlerToolbarAction = async (key: string) => {
		switch (key) {
			case "add":
				openDrawer(true, {
					reload
				});
				break;
		}
	};
	const updateFn = (record: any) => async () => {
		openDrawer(true, {
			reload,
			record
		});
	};
	const getUseList = async () => {
		const res = await getListSimpleUsers();
		setUserList(res);
	};
	useEffect(() => {
		getUseList();
	}, []);
	const deleteFn = async (record: any) => {
		const res = await receiptsApi.receiptsDelete(record.receiptId);
		if (!res) message.error(t("kevin_ui.deleteFailed"));
		message.success(t("kevin_ui.daleteSucceessfully"));
		reload();
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
			</div>
			<CreateAndUpdata register={registerDrawer}></CreateAndUpdata>
			<Toolbar action={handlerToolbarAction} btnList={["add"]} />
		</div>
	);
};

export default index;
